#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
#include<unordered_map>
#include<cstring>
#include<math.h>
#include<stack>

using namespace std;


class Solution{
public:
    string simplifyPath(string path){
        stack<string> sp;
        int len_path = path.size();
        string temp = "";
        for(char ch: path){
            if(ch == '/'){
                if(temp == ".."){
                    if(sp.size()){
                        sp.pop();
                    }
                }else if(temp.size() != 0){
                    if(temp != "."){
                        sp.push(temp);
                    }
                }
                temp = "";
            }else{
                temp = temp + ch;
            }
        }
        if(path[len_path-1] != '/'){
            if(temp == ".."){
                if(sp.size()){
                    sp.pop();
                }
            }else if(temp.size() != 0){
                if(temp != "."){
                    sp.push(temp);
                }
            }
            temp = "";
        }

        string ret = "";
        int len = sp.size();
        if(len){
            for(int i = 0; i != len; i++){
                ret = "/"  + sp.top() + ret;
                sp.pop();
            }
        }else{
            ret = "/";
        }

        return ret;
    }
};


void test(){
    Solution s;
    string path = "/home/user/Documents/../Pictures";
    string ret = s.simplifyPath(path);
    cout << ret << endl;

    // for(int i = 0; i != ret.size(); i++){
    //     cout << ret[i] << " " << ends;
    // }
    // cout << endl;

    // for(int i = 0; i != ret.size(); i++){
    //     for(int j = 0; j != ret[i].size() - 1; j++){
    //         cout << ret[i][j] << " " << ends;
    //     }
    //     cout << ret[i][ret[i].size()-1] << endl;
    // }
    
}

void test1(){
    vector<vector<int>> num = {
        {1,3},
        {8,10},
        {2,6},
        {15,18}
    };
    sort(num.begin(), num.end());
    for(int i = 0; i != num.size(); i++){
        for(int j = 0; j != num[i].size(); j++){
            cout << num[i][j] << " " << ends;
        }
        cout << endl;
    }
}

int main(void){
    test();

    return 0;
}